<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/26
 * Time: 15:36
 */

namespace app\mqtt\controller;


use app\common\controller\Base;
use think\Db;

class Service extends Base
{

    public function index()
    {
        //定义
        $data=file_get_contents("php://input");
        $data=json_decode($data,true);
        if($data['action']=='message_acked'){
//            $this->message_acked($data);
        }
        if($data['action']=='message_publish'){
        $this->message_publish($data);
    }
        logs("mq_msg",$data);
        echo "ok";

    }

    public function test()
    {
        $fsdf='';
        $this->message_acked($fsdf);
    }
    //标记已读消息
    public function message_publish($data)
    {
//        $data=array (
//        'action' => 'message_acked',
//        'client_id' => 'mqttjs_80a616e1ea',
//        'from_client_id' => 'clientx9293670xxctr',
//        'from_username' => 'undefined',
//        'topic' => 'read_msg',
//        'qos' => 2,
//        'retain' => false,
//        'payload' => '{"id":1218,"send_account_id":150042,"content":"{\\"content\\":\\"<div style=\\\\\\"display: flex;align-items: center;\\\\\\">?<\\/div>\\"}","create_time":1569053243,"chat_id":300,"is_file":0,"file_name":"","file_size":"","file_type":"","file_url":"","accept_account_id":"150042","is_accepted":1,"accept_time":1569053243,"is_sended":1,"msg_type":"1","msg_id":"379","send_account_type":"u","accept_account_type":"u"}',
//        'ts' => 1569053246,
//    );

        try {
            // 这里是主体代码
            if($data['topic']!="read_msg"){
                return ;
            }
            $msg_id =$data['payload'];
            if($msg_id){
                $accept_time = time();
                Db::name("account_msg")
                    ->where('id',$msg_id)
                    ->update(['is_accepted' => 1,'accept_time'=>$accept_time,'is_sended'=>1]);
            }

        }  catch (\Exception $e) {
            // 这是进行异常捕获
            logs("ExceptionAcked",$e->getMessage());
            return json($e->getMessage());
        }


//        $payload = json_decode($data['payload'],true);
//            if(isset($payload['msg_id'])&&isset($payload['id'])){
//                $accept_time = time();
//                Db::name("account_msg")
//                    ->where('id',$payload['id'])
//                    ->update(['is_accepted' => 1,'accept_time'=>$accept_time,'is_sended'=>1]);
//            }


    }
    //已读消息
    public function message_acked($data)
    {
//        $data=array (
//        'action' => 'message_acked',
//        'client_id' => 'mqttjs_80a616e1ea',
//        'from_client_id' => 'clientx9293670xxctr',
//        'from_username' => 'undefined',
//        'topic' => 'msg/300/u_150042',
//        'qos' => 2,
//        'retain' => false,
//        'payload' => '{"id":1218,"send_account_id":150042,"content":"{\\"content\\":\\"<div style=\\\\\\"display: flex;align-items: center;\\\\\\">?<\\/div>\\"}","create_time":1569053243,"chat_id":300,"is_file":0,"file_name":"","file_size":"","file_type":"","file_url":"","accept_account_id":"150042","is_accepted":1,"accept_time":1569053243,"is_sended":1,"msg_type":"1","msg_id":"379","send_account_type":"u","accept_account_type":"u"}',
//        'ts' => 1569053246,
//    );

        try {
            // 这里是主体代码
            $payload = json_decode($data['payload'],true);
            if(isset($payload['msg_id'])&&isset($payload['id'])){
                $accept_time = time();
                Db::name("account_msg")
                    ->where('id',$payload['id'])
                    ->update(['is_accepted' => 1,'accept_time'=>$accept_time,'is_sended'=>1]);
            }

        }  catch (\Exception $e) {
            // 这是进行异常捕获
            logs("ExceptionAcked",$e->getMessage());
            return json($e->getMessage());
        }


//        $payload = json_decode($data['payload'],true);
//            if(isset($payload['msg_id'])&&isset($payload['id'])){
//                $accept_time = time();
//                Db::name("account_msg")
//                    ->where('id',$payload['id'])
//                    ->update(['is_accepted' => 1,'accept_time'=>$accept_time,'is_sended'=>1]);
//            }


    }


}